package com.dodoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dodoke.dto.RetailWarehouseDTO;
import com.dodoke.entity.RetailWarehouse;
import com.dodoke.service.IRetailWarehouseService;
import com.dodoke.utils.AjaxResult;
import com.dodoke.utils.HttpStatus;
import com.dodoke.vo.RetailWarehouseListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 仓库信息控制层
 */
@Slf4j
@RestController
@RequestMapping("/retail/warehouse")
public class RetailWarehouseController {
    @Autowired
    private IRetailWarehouseService retailWarehouseService;

    /**
     * 仓库信息分页查询
     * @param currentPage 当前页码
     * @param pageSize 每页显示条数
     * @param retailWarehouseDTO 模糊查询参数
     * @return
     */
    @PostMapping("getListByPage/{currentPage}/{pageSize}")
    public AjaxResult getListByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize, @RequestBody RetailWarehouseDTO retailWarehouseDTO){
        return AjaxResult.success("获取仓库分页数据成功",retailWarehouseService.getListByPage(currentPage, pageSize, retailWarehouseDTO));
    }

    /**
     * 新增仓库信息
     * @param retailWarehouse 仓库信息参数
     * @return
     */
    @PostMapping
    public AjaxResult addRetailWarehouse(@Validated @RequestBody RetailWarehouse retailWarehouse){
        // 判断仓库名称是否存在
        RetailWarehouse warehouseByName = retailWarehouseService.getOne(new LambdaQueryWrapper<RetailWarehouse>().eq(RetailWarehouse::getName, retailWarehouse.getName()));
        if (warehouseByName != null){
            return AjaxResult.error(HttpStatus.ERROR,"仓库名称已存在");
        }
        // 判断仓库编号是否存在
        if (retailWarehouse.getNo() != null && !retailWarehouse.getNo().isEmpty()){
            RetailWarehouse warehouseByNo = retailWarehouseService.getOne(new LambdaQueryWrapper<RetailWarehouse>().eq(RetailWarehouse::getNo, retailWarehouse.getNo()));
            if (warehouseByNo != null){
                return AjaxResult.error(HttpStatus.ERROR,"仓库编号已存在");
            }
        }
        int resCount = retailWarehouseService.saveRetailWarehouse(retailWarehouse);
        if (resCount <= 0){
            return AjaxResult.error(HttpStatus.ERROR,"仓库信息添加失败");
        }
        return AjaxResult.success("新增仓库信息成功");
    }

    /**
     * 根据id删除仓库信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id){
        boolean isRemove = retailWarehouseService.removeById(id);
        return isRemove ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    /**
     * 修改仓库信息
     * @param retailWarehouse 仓库信息参数
     * @return
     */
    @PutMapping
    public AjaxResult editRetailWarehouse(@Validated @RequestBody RetailWarehouse retailWarehouse){
        RetailWarehouseListVO retailWarehouseListVO = retailWarehouseService.getById(retailWarehouse.getId());
        // 判断仓库名称是否存在
        RetailWarehouse warehouseByName = retailWarehouseService.getOne(new LambdaQueryWrapper<RetailWarehouse>().eq(RetailWarehouse::getName, retailWarehouse.getName()));
        if (warehouseByName != null && !warehouseByName.getId().equals(retailWarehouseListVO.getId())){
            return AjaxResult.error(HttpStatus.ERROR,"仓库名称已存在");
        }
        // 判断仓库编号是否存在
        if (retailWarehouse.getNo() != null && !retailWarehouse.getNo().isEmpty()){
            RetailWarehouse warehouseByNo = retailWarehouseService.getOne(new LambdaQueryWrapper<RetailWarehouse>().eq(RetailWarehouse::getNo, retailWarehouse.getNo()));
            if (warehouseByNo != null && !warehouseByNo.getId().equals(retailWarehouseListVO.getId())){
                return AjaxResult.error(HttpStatus.ERROR,"仓库编号已存在");
            }
        }
        int resCount = retailWarehouseService.editRetailWarehouse(retailWarehouse);
        if (resCount <= 0){
            return AjaxResult.error(HttpStatus.ERROR,"仓库信息修改失败");
        }
        return AjaxResult.success("修改仓库信息成功");
    }

    /**
     * 根据id获取仓库信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id){
        return AjaxResult.success("获取仓库信息成功",retailWarehouseService.getById(id));
    }

}
